
namespace SB2.Core;

public class AppSugarRepository<T> : SugarRepository<T>, IScoped where T : class, IEntityHasCreatedUser, IEntityHasUpdatedUser, new()
{
    public AppSugarRepository(ISqlSugarClient context) : base(context)//注意这里要有默认值等于null
    {
        base.Context = context;
    }

    public override Task<bool> InsertAsync(T insertObj)
    {
        insertObj.CreateUserID = UserId;
        insertObj.CreateTime = DateTime.Now;
        return base.InsertAsync(insertObj);
    }
    public override Task<bool> InsertRangeAsync(List<T> insertObjs)
    {
        insertObjs.ForEach(insertObj =>
{
    insertObj.CreateUserID = UserId;
    insertObj.CreateTime = DateTime.Now;
});
        return base.InsertRangeAsync(insertObjs);
    }
    public override async Task<long> InsertReturnBigIdentityAsync(T insertObj)
    {
        insertObj.CreateUserID = UserId;
        insertObj.CreateTime = DateTime.Now;
        Console.WriteLine(insertObj);
        return await base.InsertReturnBigIdentityAsync(insertObj);
    }

    public override async Task<int> InsertReturnIdentityAsync(T insertObj)
    {
        insertObj.CreateUserID = UserId;
        insertObj.CreateTime = DateTime.Now;
        return await base.InsertReturnIdentityAsync(insertObj);
    }
    public override async Task<bool> UpdateAsync(T updateObj)
    {
        updateObj.UpdateTime = DateTime.Now;
        updateObj.UpdateUserID = UserId;
        return await base.UpdateAsync(updateObj);
    }
    public override async Task<bool> UpdateRangeAsync(List<T> updateObjs)
    {
        updateObjs.ForEach(updateObj =>
        {
            updateObj.UpdateTime = DateTime.Now;
            updateObj.UpdateUserID = UserId;
        });
        return await base.UpdateRangeAsync(updateObjs);
    }

    public override async Task<bool> UpdateRangeAsync(T[] updateObjs)
    {
        foreach (var updateObj in updateObjs)
        {
            updateObj.UpdateTime = DateTime.Now;
            updateObj.UpdateUserID = UserId;
        }
        return await base.UpdateRangeAsync(updateObjs);
    }

}